搜索 K
Appearance
博客正在加载中...
Appearance
介绍下使用第三方项目,来将监控数据展示
该项目是开源的:codecentric/spring-boot-admin,并且有丰富的文档:Spring Boot Admin – doc
简单来说就是可以新建一个项目,专门用来监控其他微服务项目,我们可以称之为监控系统;监控系统会定期向其他微服务获取监控信息,并展示。
为此,我们新建一个项目
主要选择 web 开发场景即可,其他的按需选择:

根据 文档,我们引入依赖:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.7.10</version>
</dependency>注意,该依赖的版本要和 SpringBoot 的版本对应,文档是这样说的:
In the Spring Boot Admin Server App, the Spring Boot Admin's version matches the major and minor versions of Spring Boot.
Spring Boot Version Spring Boot Admin 2.6 2.6.Y 2.7 2.7.Y 3.0 3.0.Y Nevertheless, it is possible to monitor any version of a Spring Boot service independently of the underlying Spring Boot version in the service. Hence, it is possible to run Spring Boot Admin Server version 2.6 and monitor a service that is running on Spring Boot 2.3 using Spring Boot Admin Client version 2.3.
并且在主类上,加上注解 @EnableAdminServer
package com.peterjxl.learnspringbootwebadminserver;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableAdminServer // 开启监控
public class LearnSpringBootWebAdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(LearnSpringBootWebAdminServerApplication.class, args);
}
} 为了防止端口冲突,可以修改下配置文件:
server.port=8888 启动项目,效果:

接下来配置监控微服务。
首先在之前的 admin 项目中,引入客户端的依赖:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.3.1</version>
</dependency> 并且配置监控系统的地址:
spring:
application:
name: boot-admin-client # 服务名
boot:
admin:
client:
url: http://localhost:8888
instance:
prefer-ip: true # 注册时使用IP而不是主机名重启项目,然后访问应用墙:

然后单击中间的应用,可以看到详细信息:

还能看运行时指标,例如 gc 暂停次数:
